package parsing.yago;

import java.net.URL;
import java.util.Collection;
import java.util.concurrent.Future;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import core.objects.Book;
import core.threads.TaskManager;
import core.threads.ThreadCallable;

public class WikipediaThumbnailURLGrabber {

    private static final String WIKI_PREFIX = "http://en.wikipedia.org/wiki/";
    private static final String WIKI_BOOK_SOURCE = "/wiki/Special:BookSources/";
    private TaskManager taskManager;

    public WikipediaThumbnailURLGrabber() {
        taskManager = new TaskManager();
    }

    public void grab() {

        Collection<Book> books = ParsingEnvironment.getBooks().values();
        for (final Book book : books) {
            if (book.getWikipediaURL() == null) {
                book.setWikipediaURL(WIKI_PREFIX + book.getName().replace(' ', '_').replace("\\", ""));
            }

            taskManager.addTask(new ThreadCallable<Future<Void>, Void>() {
                @Override
                public Void call() throws Exception {
                    Document doc = Jsoup.connect(book.getWikipediaURL()).get();
                    Elements link = doc.select("img[src$=.jpg]");
                    if (link.size() > 0) {
                        String imageLink = link.get(0).attr("src");
                        URL imageURL = new URL("http:" + imageLink);
                        synchronized (book) {
                            book.setThumbnailURL(imageURL);
                        }
                        if (book.getISBN() == null) {
                            Elements isbn = doc.select("[href*=Special:BookSources]");
                            if (isbn.size() > 0) {
                                synchronized (book) {
                                    book.setISBN(isbn.get(0).attr("href").replace(WIKI_BOOK_SOURCE, "").replace("-", ""));
                                }
                            }
                        }
                    }
                    setArgument(null);
                    return null;
                }
            });

        }

    }

    public void waitForAllTasksToBeDone() {
        taskManager.waitForAllTasksToBeDone();
    }
}
